﻿using Raffles.OA.Utility;
using Raffles.OA.Data;
using Mapster;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Raffles.OA.WebApp.Areas.Manage.Controllers
{
    public class ConfigController : BaseController<Config>
    {
        /// <summary>
        /// 获取参数分页列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public ActionResult GetPageListJson(BaseSearchParam param, Pagination pagination)
        {
            int totalCount = 0;
            var configs = _dbContext.Queryable<Config>()
                                    .Where(x => !x.DeletedMark)
                                    .WhereIF(!param.KeyWord.IsEmpty(), x => x.Name == param.KeyWord.Trim()) 
                                    .ToPageList(pagination.PageIndex, pagination.PageSize, ref totalCount);
            return Content(DataResult<List<Config>>.PageResult(configs, totalCount).ToJson());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Form(int? id)
        {
            Config config = new Config();
            if (id != null)
            {
                config = _dbContext.Queryable<Config>().InSingle(id);
            }
            return View(config);
        }

        /// <summary>
        /// 参数保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveForm(ConfigDto configDto)
        {
            try
            {
                Config config = null;
                if (configDto.Id == null)
                {
                    if (_dbContext.Queryable<Config>().Any(x => !x.DeletedMark && x.Code == configDto.Code))
                    {
                        return Json(DataResult.Error("参数键名不允许重复"));
                    }
                    config = new Config();
                    configDto.Adapt(config); 
                    config.CreatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Insertable<Config>(config).ExecuteCommand();
                }
                else
                {
                    if (_dbContext.Queryable<Config>().Any(x => !x.DeletedMark && x.Code == configDto.Code && x.Id != configDto.Id))
                    {
                        return Json(DataResult.Error("参数键名不允许重复"));
                    }
                    config = _dbContext.Queryable<Config>().InSingle(configDto.Id);
                    configDto.Adapt(config);
                    config.UpdatedOn = DateTime.Now;
                    config.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Updateable<Config>(config).ExecuteCommand();
                }
                CacheHelper.RemoveCache(config.Code);
                CacheHelper.SetCache(config.Code, config.Value);
                return configDto.Id == null ? Json(DataResult.Success($"新增成功")) : Json(DataResult.Success($"修改成功"));
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"保存失败,失败原因:{ex.Message}"));
            }
        }

        /// <summary>
        /// 参数删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteData(int id)
        {
            try
            {
                Config config = _dbContext.Queryable<Config>().InSingle(id);
                config.DeletedMark = true;
                config.UpdatedOn = DateTime.Now;
                config.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                _dbContext.Updateable<Config>(config).ExecuteCommand();
                CacheHelper.RemoveCache(config.Code);
                return Json(DataResult.Success("删除成功"));
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"删除失败,失败原因:{ex.Message}"));
            }
        }
    }
}